package cn.lopr.im.android.mvp.presenter;

import java.util.List;

import cn.lopr.im.android.bean.Article;
import cn.lopr.im.android.bean.ArticleComment;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IArticleViewContact;
import cn.lopr.im.android.mvp.contact.ICommunityContact;
import cn.lopr.im.android.mvp.model.CommunityModel;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2021/5/23 13:24
 */
public class ArticleViewPresenterImpl implements IArticleViewContact.IArticleViewPresenter {
    private String TAG;
    private final IArticleViewContact.IArticleViewView articleViewView;
    private final ICommunityContact.ICommunityModel communityModel;

    public ArticleViewPresenterImpl(IArticleViewContact.IArticleViewView articleViewView) {
        this.articleViewView = articleViewView;
        this.communityModel = CommunityModel.getInstance();
    }

    @Override
    public void getArticle(String token) {
        communityModel.getArticle(token, articleViewView.getArticleId(), new OnCallBackListener<CallbackResult<ArticleVo>, String>() {
            @Override
            public void success(CallbackResult<ArticleVo> data) {
                articleViewView.getArticleSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {

            }
        });
    }

    @Override
    public void getComment(String token, int page, int size) {
        if (!UiUtils.isNetworkAvailable()) {
            articleViewView.showNetworkError();
            return;
        }
        communityModel.getArticleComment(token, articleViewView.getArticleId(), page, size, new OnCallBackListener<CallbackResult<List<ArticleCommentVo>>, String>() {
            @Override
            public void success(CallbackResult<List<ArticleCommentVo>> data) {
                articleViewView.getCommentSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                articleViewView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void updateArticleCommentNum(String token) {
        if (!UiUtils.isNetworkAvailable()) {
            articleViewView.showNetworkError();
            return;
        }
        Article article = new Article();
        article.setId(articleViewView.getArticleId());
        article.setVisits(1);
        communityModel.updateArticle(token, article, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
            }

            @Override
            public void failure(String errMsg) {
            }
        });

    }

    @Override
    public void addComment(String token) {
        if (!UiUtils.isNetworkAvailable()) {
            articleViewView.showNetworkError();
            return;
        }

        ArticleComment articleComment = new ArticleComment();
        articleComment.setComment(this.articleViewView.getComment());
        articleComment.setArticleId(this.articleViewView.getArticleId());
        articleComment.setUserId(this.articleViewView.getUserId());

        String replyCommentId = this.articleViewView.getReplyCommentId();
        if (replyCommentId != null) {
            articleComment.setParentId(replyCommentId);
        }
        communityModel.addArticleComment(token, articleComment, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
                articleViewView.addCommentSuccess();
            }

            @Override
            public void failure(String errMsg) {
                articleViewView.showVerifyFailed(errMsg);
            }
        });
    }
}
